package com.stylefeng.guns.modular.system.task;

import java.util.Date;
import java.util.Hashtable;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.ParamsMapper;
import com.stylefeng.guns.modular.system.model.Params;
import com.stylefeng.guns.modular.system.model.YdayGpc;
import com.stylefeng.guns.modular.system.service.IYdayGpcService;

@Service
public class YdayImportTask {

	@Autowired
	private ParamsMapper paramsMapper;
	
	@Autowired
	private IYdayGpcService ydayGpcService;
	
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			Params params = new Params();
			params.setgUserid("387065");
			params = paramsMapper.selectOne(params);
			if(params == null){
				return;
			}
			Hashtable<String, String> paramsMaps = new Hashtable<String, String>();
			paramsMaps.put("a", KpMethod.APP_A.getMethod());
			paramsMaps.put("c", "ZhiShuRanking");
			paramsMaps.put("PlateID", "801900");
			paramsMaps.put("apiv", "w11");
			paramsMaps.put("PhoneOSNew", "2");
			paramsMaps.put("Order", "1");
			paramsMaps.put("Type", "6");
			paramsMaps.put("st", "100");
			paramsMaps.put("Token", params.getgToken());
			paramsMaps.put("UserID", params.getgUserid());
			paramsMaps.put("Date", DateUtil.getDay());
			paramsMaps.put("Index", "1");
			paramsMaps.put("old", "1");
			String result = HttpsClient.postSSL(ImportContants.getKpAppDataPathStr(), paramsMaps,false);
			if(StringUtils.isNotEmpty(result)){
				JSONObject jsonObject =  JSON.parseObject(result);
				if(jsonObject.getIntValue("errcode") == 0){
					JSONArray jsonArray  = JSON.parseArray(jsonObject.getString("list"));
					for(int i=0;i<jsonArray.size();i++){
						String value = StringUtils.substringBetween(jsonArray.getString(i), "[", "]");
						String[] values = StringUtils.split(value, ",");
						if(StringUtils.contains(values[1], "*")
								|| StringUtils.contains(values[1], "ST")
								|| StringUtils.contains(values[1], "退市")){
							continue;
						}
						YdayGpc  ydayGpc = new YdayGpc();
						ydayGpc.setgScode(StringUtils.remove(values[0], "\""));
						ydayGpc.setgTime(new Date());
						
						ydayGpc = ydayGpcService.selectByDate(ydayGpc);
						if(ydayGpc == null){
							ydayGpc = new YdayGpc();
							ydayGpc.setgScode(StringUtils.remove(values[0], "\""));
							ydayGpc.setgTime(new Date());
							ydayGpc.setgCje(Double.valueOf(values[7]));
							ydayGpc.setgDis(StringUtils.remove(values[24], "\""));
							ydayGpc.setgDisOrder(getOrder(StringUtils.remove(values[24], "\"")));
							String lb = StringUtils.remove(values[23], "\"");
							if(StringUtils.isEmpty(lb)){
								ydayGpc.setgLbCount(0);
							}else{
								if(StringUtils.contains(lb, "首板")){
									ydayGpc.setgLbCount(1);
								}else if(StringUtils.contains(lb, "板")){
									ydayGpc.setgLbCount(Integer.valueOf(StringUtils.substring(lb, StringUtils.indexOf(lb, "连") -1, StringUtils.indexOf(lb, "连"))));
								}else{
									ydayGpc.setgLbCount(1);
								}
							}
							ydayGpc.setgLb(lb);
							ydayGpc.setgHsl(Double.valueOf(values[8]));
							ydayGpc.setgLt(StringUtils.remove(values[9], "\""));
							ydayGpc.setgPrice(Double.valueOf(values[5]));
							ydayGpc.setgReason(StringUtils.remove(values[4], "\""));
							ydayGpc.setgSjlt(Double.valueOf(values[10]));
							ydayGpc.setgZdf(Double.valueOf(values[6]));
							ydayGpc.setgZlbuy(Double.valueOf(values[11]));
							ydayGpc.setgZlje(Double.valueOf(values[13]));
							ydayGpc.setgZlsell(Double.valueOf(values[12]));
							ydayGpc.setgZs(Double.valueOf(values[9]));
							ydayGpcService.insert(ydayGpc);
						}else{
							ydayGpc.setgCje(Double.valueOf(values[7]));
							ydayGpc.setgDis(StringUtils.remove(values[24], "\""));
							String lb = StringUtils.remove(values[23], "\"");
							if(StringUtils.isEmpty(lb)){
								ydayGpc.setgLbCount(0);
							}else{
								if(StringUtils.contains(lb, "首板")){
									ydayGpc.setgLbCount(1);
								}else if(StringUtils.contains(lb, "板")){
									ydayGpc.setgLbCount(Integer.valueOf(StringUtils.substring(lb, StringUtils.indexOf(lb, "连") -1, StringUtils.indexOf(lb, "连"))));
								}else{
									ydayGpc.setgLbCount(1);
								}
							}
							ydayGpc.setgLb(lb);
							ydayGpc.setgHsl(Double.valueOf(values[8]));
							ydayGpc.setgDisOrder(getOrder(StringUtils.remove(values[24], "\"")));
							ydayGpc.setgLt(StringUtils.remove(values[9], "\""));
							ydayGpc.setgPrice(Double.valueOf(values[5]));
							ydayGpc.setgReason(StringUtils.remove(values[4], "\""));
							ydayGpc.setgSjlt(Double.valueOf(values[10]));
							ydayGpc.setgZdf(Double.valueOf(values[6]));
							ydayGpc.setgZlbuy(Double.valueOf(values[11]));
							ydayGpc.setgZlje(Double.valueOf(values[13]));
							ydayGpc.setgZlsell(Double.valueOf(values[12]));
							ydayGpc.setgZs(Double.valueOf(values[9]));
							ydayGpcService.updateById(ydayGpc);
						}
					}
				}
			}
		}
	}
	
	
	private int getOrder(String order){
		if(StringUtils.isEmpty(order)){
			return 999;
		}
		if(StringUtils.contains(order, "龙一")){
			return 1;
		}else if(StringUtils.contains(order, "龙二")){
			return 2;
		}else if(StringUtils.contains(order, "龙三")){
			return 3;
		}else if(StringUtils.contains(order, "龙四")){
			return 4;
		}else if(StringUtils.contains(order, "龙五")){
			return 5;
		}else if(StringUtils.contains(order, "龙六")){
			return 6;
		}else if(StringUtils.contains(order, "龙七")){
			return 7;
		}else if(StringUtils.contains(order, "龙八")){
			return 8;
		}else if(StringUtils.contains(order, "龙九")){
			return 9;
		}else if(StringUtils.contains(order, "龙十")){
			return 10;
		}
		return 999;
	}
}
